#include "AnimationDelegate.h"
#include <QTimerEvent>
#include <QProcess>
#include <QWindow>
#include <windows.h>
#include <iostream>
#include <QVBoxLayout>
#include <QLabel>

#define TIMER_TIMEOUT   (1*1000)

AnimationDelegate::AnimationDelegate()
{

}



AnimationDelegate::AnimationDelegate(
    QObject *parent):
    QObject(parent)
{
    m_nTimerID = this->startTimer(TIMER_TIMEOUT);
}



AnimationDelegate::AnimationDelegate(
    QMdiArea* mdi,
    QObject* parent):
    QObject(parent)
{
    d_mdi = mdi;
    m_nTimerID = this->startTimer(TIMER_TIMEOUT);
}



AnimationDelegate::~AnimationDelegate()
{

}



void
AnimationDelegate::timerEvent(
    QTimerEvent *event)
{

    QString cmd="C:/Windows/system32/calc.exe";
    STARTUPINFO si={sizeof(si)};
    PROCESS_INFORMATION pi;
    si.dwFlags=STARTF_USESHOWWINDOW;
    si.wShowWindow=true;
/*
    bool bRet=CreateProcess(
               NULL,
               (LPWSTR)cmd.toStdWString().c_str(),
               NULL,
               NULL,
               FALSE,
               CREATE_NEW_CONSOLE,
               NULL,
               NULL,&si,&pi);*/
    QProcess* process = new QProcess;
    process->start(cmd);
    WId wid = (WId)FindWindow(L"CalcFrame",NULL);
    //WId wid = (WId)FindWindow(L"Rgui Workspace",NULL);

    QWindow* m_window;
    m_window = QWindow::fromWinId(wid);
    if (m_window== NULL)
    {
        std::cout << "win null" << std::endl;
    }
    else
    {
         std::cout << "win not null" << std::endl;
    }
    //m_window->setFlags(m_window->flags() | Qt::CustomizeWindowHint | Qt::WindowTitleHint); //
    QWidget* widget = QWidget::createWindowContainer(m_window, d_mdi);

    QVBoxLayout* la = new QVBoxLayout;
    QLabel* lb = new QLabel(tr ("kkkkk"));
    la->addWidget(lb);
    //widget->setLayout(la);

    d_mdi->addSubWindow(widget);
    d_mdi->setWindowTitle(tr ("bbb"));

    if(event->timerId() == m_nTimerID)
    {
        handleTimeout();
    }
}



void
AnimationDelegate::handleTimeout()
{
    //qDebug()<<"Enter timeout processing function\n";
    killTimer(m_nTimerID);
}

